package io.github.biezhi.elves.spider;

import java.net.InetSocketAddress;
import java.net.Proxy;
import java.net.SocketTimeoutException;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.ListIterator;
import java.util.Map;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.alibaba.fastjson.JSONObject;

import io.github.biezhi.elves.bidding.model.BiddingKeywordDTO;
import io.github.biezhi.elves.bidding.model.BiddingListDTO;
import io.github.biezhi.elves.bidding.model.BiddingListQuery;
import io.github.biezhi.elves.bidding.model.DownloadBiddingHtmlDTO;
import io.github.biezhi.elves.bidding.model.DownloadFileDTO;
import io.github.biezhi.elves.bidding.model.RecordBiddingNoticeDTO;
import io.github.biezhi.elves.bidding.model.ResolveBiddingDTO;
import io.github.biezhi.elves.bidding.model.ResolveBiddingQuery;
import io.github.biezhi.elves.bidding.model.RecordNoticeQuery;
import io.github.biezhi.elves.common.BiddingNoticeOperationMode;
import io.github.biezhi.elves.common.BiddingStockSave;
import io.github.biezhi.elves.common.Constants;
import io.github.biezhi.elves.common.MapConstants;
import io.github.biezhi.elves.common.SiteNameConstants;
import io.github.biezhi.elves.common.SpecialNameMap;
import io.github.biezhi.elves.model.BiddingDetailApi;
import io.github.biezhi.elves.model.BiddingInfoResultApi;
import io.github.biezhi.elves.model.BiddingQuery;
import io.github.biezhi.elves.ok.http.FastHttpClient;
import io.github.biezhi.elves.service.BidService;
import io.github.biezhi.elves.utils.DateUtils;
import io.github.biezhi.elves.utils.FileUtil;
import io.github.biezhi.elves.utils.StringUtils;

/**
 * 招标信息网爬虫解析
 *
 */
@Component
public class BiddingInformationNetworkSpider {
	@Autowired
	private BidService bidService;
	
	/**
	 * 开始执行爬取投标公告数据
	 * @throws Exception
	 */
	//页码，默认第一页
	static int pageIndex = 1;
	//末页页码
	static int lastPageIndex = 0;
	//关键词类型
	static int keywordType = 2;
	//是否完成保存记录投标网公告标题数据
	static Boolean finished = false;
	Long sleepTime = 1000L;
	static int listPurchaseModeIndex = 0;
	public void startBid() throws Exception{
		biddingNoticeData(pageIndex);
	}
	
	/**
	 * 爬取投标公告数据
	 * @param pageNo		参数说明:页号
     * @param pageSize		参数说明:每页显示记录数
	 */
	private void biddingNoticeData(int pageIndex) throws Exception{
		System.out.println("========开始请求，第" + pageIndex + "页=====");
		String siteNameEn = SiteNameConstants.ZBYJW_EN;
		siteNameEn = SiteNameConstants.CBW_EN;
		String siteName = StringUtils.getValueForKey(siteNameEn, MapConstants.SITE_BY_EN_MAP);
		
//		saveBiddingNotice("", pageIndex, siteName, sleepTime);
//		saveRecordCbwNotice(keywordType, siteName);
		
//		saveDownloadBiddingHtml("", siteName);
		
		//解析招标预警网公告数据
//		resolveWinBiddingHtml(siteName);
		//解析采标网公告数据
		resolveCbwNoticeData(siteName);
//		updateDownloadBiddingForHtmlContent();
//		updateDownloadBiddingForIframeHtmlContent();
		
//		listResolveBiddingPage(siteName);
	}
	
	/**
	 * 更新公告数据表htmlContent数据
	 * @throws Exception
	 */
	private void updateDownloadBiddingForHtmlContent() throws Exception {
		List<DownloadBiddingHtmlDTO> listBiddingHtmls = this.bidService.listDownloadBiddingHtmlIsNullHtmlContent();
		for (DownloadBiddingHtmlDTO biddingHtml : listBiddingHtmls) {
			String url = "http://10.18.160.100:9090/data-manager/api/notice/htmlCode/" + biddingHtml.getRecordId() + "?type=update&searchType=stockNoticeRepeat";
			String result = getHtmlContent(url);
			biddingHtml.setHtmlContent(result);
			this.bidService.updateDownloadBiddingForHtmlContent(biddingHtml);
		}
		System.err.println("已跑完，终止程序");
		System.exit(0);
	}
	
	/**
	 * 更新公告数据表iframeHtmlContent数据
	 * @throws Exception
	 */
	private void updateDownloadBiddingForIframeHtmlContent() throws Exception {
		String siteUrl = "http://www.bidcenter.com.cn";
		List<DownloadBiddingHtmlDTO> listBiddingHtmls = this.bidService.listDownloadBiddingHtmlIsNullIframeHtmlContent();
		if (listBiddingHtmls != null && listBiddingHtmls.size() > 0) {
			for (DownloadBiddingHtmlDTO biddingHtml : listBiddingHtmls) {
				String htmlCode = biddingHtml.getHtmlContent();
				Document doc = Jsoup.parse(htmlCode);
				Elements iframeElements = doc.select("#iframe_content");
				if (iframeElements != null && iframeElements.size() > 0) {
					String iframeString = iframeElements.attr("src");
					String iframeSrc = iframeString;
					if (!iframeSrc.contains("http://www.bidcenter.com.cn")) {
						iframeSrc = siteUrl + iframeString;
					}
					String result = getHtmlContent(iframeSrc);
					biddingHtml.setIframeHtmlContent(result);
					this.bidService.updateDownloadBiddingForIframeHtmlContent(biddingHtml);
				}
			}
			updateDownloadBiddingForIframeHtmlContent();
		}
		System.err.println("已跑完，终止程序");
		System.exit(0);
	}
	
	/**
	 * 保存记录投标公告标题数据
	 * @param keyword
	 * @param pageIndex
	 * @param sleepTime
	 * @throws Exception
	 */
	private void saveBiddingNotice(String keyword, Integer pageIndex, String siteName, Long sleepTime) throws Exception {
		String result = postHtmlContent(keyword, pageIndex);
		BiddingInfoResultApi biddingInfoResult = null;
		try {
			biddingInfoResult = JSONObject.parseObject(result, BiddingInfoResultApi.class);
		} catch (Exception e) {
			System.out.println("出现异常==>" + StringUtils.getErrorInfoFromException(e));
			System.out.println();
			System.err.println("输出调用结果==>" + biddingInfoResult);
			sleepTime();
			saveBiddingNotice(keyword, pageIndex, siteName, sleepTime);
		}
		
		if (!"yes".equals(biddingInfoResult.getResult())) {
			System.out.println("出现频繁==>");
			System.out.println();
			System.err.println("输出调用结果==>" + biddingInfoResult);
			sleepTime();
			resetProxyIp();
			saveBiddingNotice(keyword, pageIndex, siteName, sleepTime);
		}
		
		if (biddingInfoResult != null && "yes".equals(biddingInfoResult.getResult())) {
			Boolean compareDateResult = false;
			for (int i = 0; i < biddingInfoResult.getResultlist().size(); i++) {
				BiddingDetailApi detail = biddingInfoResult.getResultlist().get(i);
				
				if (detail.getTitle().contains("废标")) {
					continue;
				}
				
				String pubdate = detail.getPubdate();
				compareDateResult = compareDate(pubdate, "");
				if (compareDateResult) {
					//比较发布日期，如果发布日期小于2018年，则终止程序
					System.out.println("终止程序，发布日期为：" + pubdate);
					System.exit(0);
					break;
				} else {
					//创建记录投标公告标题数据对象
					RecordBiddingNoticeDTO recordBiddingNotice = null;
					try {
						recordBiddingNotice = createRecordBiddingNoticeDTO(keyword, detail, pageIndex);
						if (recordBiddingNotice != null) {
							System.out.println("要插入的数据：" + recordBiddingNotice);
							this.bidService.saveRecordBiddingNotice(recordBiddingNotice);
						} else {
							continue;
						}
					} catch (Exception e) {
						e.printStackTrace();
						continue;
					}
				}
			}
			
//			saveDownloadBiddingHtml(keyword, siteName);
			pageIndex++;
			biddingNoticeData(pageIndex);
		}
	}
	
	/**
	 * 保存公告正文内容数据到数据库
	 * @param keyword			参数说明：关键字
	 * @param siteName		参数说明：网站名称
	 * @throws Exception
	 */
	private void saveDownloadBiddingHtml(String keyword, String siteName) throws Exception {
		int index = 1;
		
		Constants.IS_PROXY = true;
//		Constants.IS_PROXY_IP = true;
        
		List<RecordBiddingNoticeDTO> listRecordBiddingNotice = this.bidService.listRecordBiddingNotice(new RecordNoticeQuery(BiddingNoticeOperationMode.ZERO, siteName));
		if(listRecordBiddingNotice != null && listRecordBiddingNotice.size() > 0) {
			for(RecordBiddingNoticeDTO recordBiddingNotice : listRecordBiddingNotice) {
				System.err.println("====正在读取第" + index + "条数据=====");
				
				BiddingQuery query = new BiddingQuery(recordBiddingNotice);
				
				//如果大于0，则表示已经爬取了数据，不再爬取，继续循环
				Integer existBiddingCount = this.bidService.countDownloadBiddingHtmlById(query);
				if (existBiddingCount > 0) {
					System.out.println("downloadHtml已存在：" + query);
					continue;
				}
				
				String detailUrl = recordBiddingNotice.getNoticeDetailUrl();
				String result = getHtmlContent(detailUrl);
				if (StringUtils.isNullOrEmpty(result)) {
					sleepTime();
					result = getHtmlContent(detailUrl);
				}
				Document doc = Jsoup.parse(result);
				Elements elements = doc.select(".artbody");
				if (elements == null || elements.size() == 0) {
					elements = doc.select(".c_con");
				}
				
				//采标网出现频繁页面判断
				if (siteName.equals(SiteNameConstants.CBW_NAME)) {
					if (elements == null || elements.size() == 0) {
						elements = doc.select("#myDiv");
						Elements iframeElements = doc.select("#iframe_content");
						if ((elements == null || elements.size() == 0) || (iframeElements == null || iframeElements.size() == 0)) {
							while(true) {
								elements = doc.select(".g-lc-hint-container > b");
								String message = "尊敬的用户您好，您的访问出现异常，为确认本次访问为正常用户行为，需要您协助验证：";
								if (elements != null && elements.size() > 0) {
									String txtResult = elements.get(0).text();
									if (message.equals(txtResult)) {
										System.out.println("出现拖动模块操作页面，休眠5秒。。。");
										sleepTime(5000L);
									}
									result = getHtmlContent(detailUrl);
									doc = Jsoup.parse(result);
								} else {
									elements = doc.select("#myDiv");
									break;
								}
							}
						}
						if (elements == null || elements.size() == 0) {
							elements = doc.select(".txt_1");
							String message = "对不起，由于目前访问人数太多，我们的系统暂时无法及时向您提供服务，请稍后再试！";
							String txtResult = elements.get(0).text();
							if (message.equals(txtResult)) {
								continue;
							}
						}
						result = result.replace("\"/zhaobiao", "\"http://www.bidcenter.com.cn/zhaobiao")
								.replace("\"/js", "\"http://www.bidcenter.com.cn/js")
								.replace("id=\"iframe_content\"", "id=\"iframe_content\" style=\"height: 3000px; border: 0px; overflow-y: hidden;\"");
						FileUtil.createFile("test2", result, "html");
					}
				} else if (siteName.equals(SiteNameConstants.ZBYJW_NAME)) {
					//招标预警网出现频繁页面判断
					
					if (elements == null || elements.size() == 0) {
						elements = doc.getElementsByTag("title");
						String message = "您查看得太频繁了哦，请休息一下再查看！";
						String txtResult = elements.text();
						if (message.equals(txtResult)) {
							Constants.IS_PROXY = true;
							Constants.IS_PROXY_IP = true;
							
							result = getHtmlContent(detailUrl);
							doc = Jsoup.parse(result);
							
							elements = doc.select(".artbody");
							if (elements == null || elements.size() == 0) {
								elements = doc.select(".c_con");
							}
						}
					}
				}
				if (elements == null || elements.size() == 0) {
					System.out.println("elements为NULL，进入死循环请求链接：" + detailUrl + "，直到能获取到artbody不为null为止，跳出循环");
					while (true) {
						result = getHtmlContent(detailUrl);
						doc = Jsoup.parse(result);
						elements = doc.select(".artbody");
						if (elements == null || elements.size() == 0) {
							elements = doc.select(".c_con");
						}
						if(elements != null && elements.size() > 0) {
							break;
						} else {
							resetProxyIp();
						}
					}
				}
				if(elements != null && elements.size() > 0) {
					DownloadBiddingHtmlDTO downloadBiddingHtml = new DownloadBiddingHtmlDTO();
					downloadBiddingHtml.setRecordId(recordBiddingNotice.getRecordId());
					downloadBiddingHtml.setNoticeType(recordBiddingNotice.getNoticeType());
					downloadBiddingHtml.setPurchaseMode(recordBiddingNotice.getPurchaseMode());
					downloadBiddingHtml.setKeyword(recordBiddingNotice.getKeyword());
					String html = result.replace("\r\n", "");
					downloadBiddingHtml.setHtmlContent(html);
					downloadBiddingHtml.setPubdate(recordBiddingNotice.getPubdate());
					
					Elements iframeElements = doc.select("#iframe_content");
					if (iframeElements != null && iframeElements.size() > 0) {
						String iframeSrc = iframeElements.attr("src");
						if (!iframeSrc.contains("http://www.bidcenter.com.cn")) {
							iframeSrc = iframeSrc.replace("/zhaobiao", "http://www.bidcenter.com.cn/zhaobiao");
						}
						String iframeHtml = getHtmlContent(iframeSrc);
						downloadBiddingHtml.setIframeHtmlContent(iframeHtml);
					}
					
					//公告详情页面正文内容
					Elements contElements = doc.select("div");
					if(contElements != null && contElements.size() > 0) {
						//查找正文中所有a标签
						Elements allAElements = contElements.select("a");
						ListIterator<Element> htmlAResultIter = allAElements.listIterator();
						
						List<DownloadFileDTO> listDownloadFile = new ArrayList<DownloadFileDTO>();
						while(htmlAResultIter.hasNext()) {
							Element get = htmlAResultIter.next();

							String fileName = get.text();
							if (StringUtils.isNullOrEmpty(fileName)) {
								continue;
							}
//							String fileUrl = website + get.attr("href");
							String fileUrl = get.attr("href");
							//获取所有a标签，没法判断是什么文件后缀(参数被加密了)，只能全部提取
							DownloadFileDTO downloadFile = new DownloadFileDTO();
							downloadFile.setFileName(fileName);
							downloadFile.setFileUrl(fileUrl);
							
							System.out.println("fileName：" + fileName);
							System.out.println("fileUrl：" + fileUrl);
							listDownloadFile.add(downloadFile);
						}
						downloadBiddingHtml.setDownloadFile(JSONObject.toJSONString(listDownloadFile));
					}
					//保存正文内容数据
					System.out.println("要保存的正文内容数据：" + downloadBiddingHtml);
					this.bidService.saveDownloadBiddingHtml(downloadBiddingHtml);
					this.bidService.updateRecordBiddingNotice("1", recordBiddingNotice.getId());
					System.out.println(JSONObject.toJSONString(downloadBiddingHtml));
				} else {
					this.bidService.updateRecordBiddingNotice("-1", recordBiddingNotice.getId());
				}
				index++;
			}
			System.err.println("根据关键字：" + keyword + "，公告类型：" + "中标公告" + "，采购方式：" + "" + "查询条件，共新增" + listRecordBiddingNotice.size() + "页，全部保存到t_download_bidding_html表中，系统自动结束程序。。。");
			System.exit(0);
		} else {
			System.err.println("没有查询到中标数据，系统自动结束程序。。。");
			System.exit(0);
		}
	}
	
	/**
	 * 创建记录投标公告标题数据DTO对象
	 * @param keyword				参数说明：关键字
	 * @param detail					参数说明：公告列表接口字段
	 * @param pageIndex			参数说明：分页页码
	 * @throws Exception
	 */
	private RecordBiddingNoticeDTO createRecordBiddingNoticeDTO(BiddingQuery query,
			Integer pageIndex) throws Exception {
		RecordBiddingNoticeDTO recordBiddingNotice = new RecordBiddingNoticeDTO();
        
		String recordId = query.getRecordId();
		String noticeType = "中标公告";
		String purchaseMode = "";
        Integer exsitBiddingCount = this.bidService.countRecordBiddingNoticeByTitle(query);
        if(exsitBiddingCount > 0) {
        	System.out.println("发布日期：" + query.getPubdate() + "，已存在数据：" + query);
        	return null;
        }
        
        recordBiddingNotice.setRecordId(recordId);
        recordBiddingNotice.setNoticeType(noticeType);
        recordBiddingNotice.setPurchaseMode(purchaseMode);
        recordBiddingNotice.setKeyword(query.getKeyword());
        recordBiddingNotice.setTitle(query.getTitle());
        recordBiddingNotice.setNoticeDetailUrl(query.getNoticeDetailUrl());
        recordBiddingNotice.setSize(pageIndex);
        recordBiddingNotice.setOperationMode(BiddingNoticeOperationMode.ZERO);
        recordBiddingNotice.setPubdate(query.getPubdate());
        return recordBiddingNotice;
	}
	
	/**
	 * 创建记录投标公告标题数据DTO对象
	 * @param keyword				参数说明：关键字
	 * @param detail					参数说明：公告列表接口字段
	 * @param pageIndex			参数说明：分页页码
	 * @throws Exception
	 */
	private RecordBiddingNoticeDTO createRecordBiddingNoticeDTO(String keyword,
			BiddingDetailApi detail, Integer pageIndex) throws Exception {
		RecordBiddingNoticeDTO recordBiddingNotice = new RecordBiddingNoticeDTO();
        
		String id = detail.getId().toString();
		String noticeType = "中标公告";
		String purchaseMode = "";
        BiddingQuery query = new BiddingQuery(id, detail.getTitle(), noticeType, purchaseMode, keyword);
        Integer exsitBiddingCount = this.bidService.countRecordBiddingNoticeByTitle(query);
        if(exsitBiddingCount > 0) {
        	System.out.println("发布日期：" + detail.getPubdate() + "，已存在数据：" + query);
        	return null;
        }
        
        recordBiddingNotice.setRecordId(id);
        recordBiddingNotice.setNoticeType(noticeType);
        recordBiddingNotice.setPurchaseMode(purchaseMode);
        recordBiddingNotice.setKeyword(keyword);
        recordBiddingNotice.setTitle(detail.getTitle());
        recordBiddingNotice.setNoticeDetailUrl(detail.getDetailUrl());
        recordBiddingNotice.setSize(pageIndex);
        recordBiddingNotice.setOperationMode(BiddingNoticeOperationMode.ZERO);
        recordBiddingNotice.setPubdate(detail.getPubdate());
        return recordBiddingNotice;
	}
	
	/**
	 * 检测公告标题是否含有中标公告关键字
	 * @param noticeTitle
	 * @return
	 */
	private Boolean checkNoticeTitle(String noticeTitle) {
		Boolean existNoticeTitle = false;
		String[] winNoticeNameKeywords = Constants.NOTICE_NAME_KEYWORDS;
		for (String winNoticeNameKeyword : winNoticeNameKeywords) {
			if (noticeTitle.contains(winNoticeNameKeyword)) {
				existNoticeTitle = true;
				break;
			}
		}
		return existNoticeTitle;
	}
	
	/**
	 * 解析中标公告HTML
	 * @throws Exception 
	 */
	//公告格式
	Integer noticeFormat = 1;
	String resolveHtml = "-1";
	public void resolveWinBiddingHtml(String siteName) throws Exception {
		List<RecordBiddingNoticeDTO> listRecordBiddingNotice = this.bidService.listRecordBiddingNotice(new RecordNoticeQuery(size, Constants.NOTICE_OPERATION_MODE, siteName));
		if (listRecordBiddingNotice == null || listRecordBiddingNotice.size() == 0) {
			System.err.println("====没有要解析的数据");
			try {
				System.err.println("=====开始：过滤重复公告数据，保存到公告存量表=======");
				//过滤重复公告数据，保存到公告存量表
				filterRepeatResolveBiddingToSaveBiddingStock(siteName);
				System.err.println("=====结束：过滤重复公告数据，保存到公告存量表=======");
				
				System.err.println("===开始休眠" + sleepTime + "毫秒......");
				Thread.sleep(sleepTime);
				System.err.println("===结束休眠，重新获取解析数据");
				resolveWinBiddingHtml(siteName);
			} catch (Exception e) {}
		}
		if(listRecordBiddingNotice != null && listRecordBiddingNotice.size() > 0) {
			for(RecordBiddingNoticeDTO recordBiddingNotice: listRecordBiddingNotice) {
				if (!checkNoticeTitle(recordBiddingNotice.getTitle())) {
					continue;
				} else {
					System.out.println("公告标题：" + recordBiddingNotice.getTitle() + ",匹配结果：" + checkNoticeTitle(recordBiddingNotice.getTitle()) + "，链接：" + recordBiddingNotice.getNoticeDetailUrl());
				}
		
				DownloadBiddingHtmlDTO noticeHtml = null;
				try {
					noticeHtml = this.bidService.downloadBiddingHtmlByRecordId(recordBiddingNotice.getRecordId());
				} catch (Exception e) {}
				
				if(noticeHtml == null) {
					continue;
				}
				if(noticeHtml != null) {
					String noticeHtmlContent = noticeHtml.getHtmlContent();
					String result = noticeHtmlContent;
					result = result.replace("&nbsp;", "").replace("<br>", "").replace("</br>", "")
//							.replace("<div style=\"LINE-HEIGHT: 23pt; TEXT-INDENT: -21pt; MARGIN: 0cm 0cm 0pt 21pt\">", "<p>")
							;
//					System.out.println(result);
					
					Elements elements = null;
					Document doc = Jsoup.parse(result);
					if (recordBiddingNotice.getNoticeDetailUrl().contains("show.job592.com")) {
						elements = doc.select(".c_swrap > div").get(1).select("p");
					} else if (recordBiddingNotice.getNoticeDetailUrl().contains("my.zbyjw.cn")) {
						try {
							elements = doc.select(".artbody > div").get(1).select("p");
						} catch (Exception e) {
							elements = doc.select(".artbody > div > p");
						}
						if (elements == null || elements.size() == 0) {
							elements = doc.select(".artbody > div > font > table > tbody > tr > td > table > tbody > tr");
							if(elements != null && elements.size() > 0) {
								noticeFormat = 2;
							}
						}
					}
					String pubdate = recordBiddingNotice.getPubdate();
					if(elements != null && elements.size() > 0) {
						BiddingListDTO winBidding = new BiddingListDTO();
						String elementsText = elements.text();
						
						//发标时间
						if (!StringUtils.isNullOrEmpty(StringUtils.regexData(elementsText, "招标公告日期：\\d{4}年\\d{1,2}月\\d{1,2}日"))) {
							String publishTimeStr = StringUtils.regexData(elementsText, "招标公告日期：\\d{4}年\\d{1,2}月\\d{1,2}日");
							if (!StringUtils.isNullOrEmpty(publishTimeStr)) {
								publishTimeStr = publishTimeStr.replace("年", "-").replace("月", "-").replace("日", "");
								publishTimeStr = publishTimeStr.replace("招标公告期：", "");
							}
							if (!StringUtils.isNullOrEmpty(publishTimeStr) && StringUtils.isNullOrEmpty(winBidding.getPublishTime())) {
								String publishDateStr = publishTimeStr;
								Date publishDate = DateUtils.stringToDate(publishDateStr, DateUtils.dateformat8);
								String publishDateString = DateUtils.dateToString(publishDate, DateUtils.dateformat14);
								winBidding.setPublishTime(publishDateString);
							}
						}
						
//						//中标金额
//						if (!StringUtils.isNullOrEmpty(StringUtils.regexData(elementsText, "￥([1-9]\\d*(\\.\\d*[0-9])?)|(0\\.\\d*[0-9])"))) {
//							String bidPrice = StringUtils.regexData(elementsText, "￥([1-9]\\d*(\\.\\d*[0-9])?)|(0\\.\\d*[0-9])");
//							if (!StringUtils.isNullOrEmpty(bidPrice) && StringUtils.isNullOrEmpty(winBidding.getBidPrice())) {
//								winBidding.setBidPrice(bidPrice);
//							}
//						}
						
						List<BiddingListDTO> winBiddingList = new ArrayList<BiddingListDTO>();
						for (Element element : elements) {
							String noticeName = "-1";
							String targetContent = element.text();
							if (StringUtils.isNullOrEmpty(targetContent)) {
								continue;
							}
							
							String customer = "";
							String prjId = "";
							String prjName = "";
							String budget = "";
							String buyType = "";
							String supplier = "";
							String bidPrice = "";
							
//							System.out.println(targetContent);
							
							if (noticeFormat == 1) {
								//中标格式1(可查询图片，目录如下：docs\noticeFormat\winBiddingNotice\noticeFormat1.png)
								if ((!targetContent.contains("采购项目编号：") && !targetContent.contains("项目编号：")
										&& (!targetContent.contains("采购项目名称：") && !targetContent.contains("项目名称："))
										&& (!targetContent.contains("采购项目预算金额（元）：") && !targetContent.contains("采购项目预算金额：")
										&& !targetContent.contains("项目预算总价：") && !targetContent.contains("预算金额:"))
										&& !targetContent.contains("采购方式：")
										&& (!targetContent.contains("中标、成交金额（元）") && !targetContent.contains("中标金额（元）")
												&& !targetContent.contains("中标金额："))
										&& (!targetContent.contains("评审日期：") && !targetContent.contains("中标日期："))
										&& (!targetContent.contains("中标供应商") && !targetContent.contains("中标单位：")
												&& !targetContent.contains("中标标的名称：")
												&& !targetContent.contains("成交供应商名称：")
												&& !targetContent.contains("中标人名称："))
										&& (StringUtils.isNullOrEmpty(StringUtils.regexData(targetContent, "受[\\s\\S]+的委托"))
												&& StringUtils.isNullOrEmpty(StringUtils.regexData(targetContent, "受 [\\s\\S]+的委托"))
												&& StringUtils.isNullOrEmpty(StringUtils.regexData(targetContent, "受[\\s\\S]+委托"))))) {
									continue;
								}
							}
							
							if (noticeFormat == 2) {
								targetContent = targetContent.replaceAll("\\s+", "").replaceAll(" ", "");
								//中标格式2(可查询图片，目录如下：docs\noticeFormat\winBiddingNotice\noticeFormat2.png)
								if (!targetContent.contains("项目编号：") && !targetContent.contains("项目名称：")
										&& !targetContent.contains("发包类型：")  && !targetContent.contains("中标单位名称：")
										&& !targetContent.contains("中标价（万元）：") && !targetContent.contains("中标时间：")) {
									continue;
								}
							}
							
							if (StringUtils.isNullOrEmpty(winBidding.getNoticeHtml())) {
								winBidding.setNoticeHtml(noticeHtmlContent);
							}
							if (StringUtils.isNullOrEmpty(winBidding.getNoticeTitle())) {
								winBidding.setNoticeTitle(recordBiddingNotice.getTitle());
							}
							if (StringUtils.isNullOrEmpty(winBidding.getNoticeName())) {
								noticeName = "中标公告";
								winBidding.setNoticeName(noticeName);
							}
							if (StringUtils.isNullOrEmpty(winBidding.getPubdate())) {
								winBidding.setPubdate(DateUtils.dateToString(DateUtils.stringToDate(pubdate, DateUtils.dateformat8), DateUtils.dateformat8));
							}
							
							if (noticeFormat == 1 || noticeFormat == 3) {
								//中标格式1
								createNoticeFormat1(winBidding, targetContent, element, elements);
							} else if (noticeFormat == 2) {
								//中标格式2
								createNoticeFormat2(winBidding, targetContent, element);
							}
							
							try {
								if (StringUtils.isNullOrEmpty(StringUtils.regexData(targetContent, "受[\\s\\S]+的委托"))
										&& StringUtils.isNullOrEmpty(StringUtils.regexData(targetContent, "受 [\\s\\S]+的委托"))
										&& StringUtils.isNullOrEmpty(StringUtils.regexData(targetContent, "受[\\s\\S]+委托"))) {
									if (StringUtils.isNullOrEmpty(winBidding.getCustomer())) {
										Elements spanElements = doc.select(".c_swrap > div > table > tbody > tr > td > span");
										if (spanElements != null && spanElements.size() > 0) {
											for (Element spanElement : spanElements) {
												String targetSpanContent = spanElement.text();
												if (targetSpanContent.contains("采购人：") && StringUtils.isNullOrEmpty(winBidding.getCustomer())) {
													if ("/".equals(customer)) {
														customer = "";
													}
													customer = targetSpanContent.replace("1.", "").replace("2.", "").replace("（三）" , "")
															.replace("采购人：", "");
													if (!StringUtils.isNullOrEmpty(customer)) {
														winBidding.setCustomer(customer);
													}
													
													//地市
													if (!StringUtils.isNullOrEmpty(customer) && StringUtils.isNullOrEmpty(winBidding.getCity())) {
														String city = matchCityByCustomer(customer);
														if (!StringUtils.isNullOrEmpty(city)) {
															winBidding.setCity(city);
														}
													}
													break;
												}
											}
										}
									}
								}
							} catch (Exception e) {}
							
							//公告内容详情链接
							if (StringUtils.isNullOrEmpty(winBidding.getBidUrl())) {
								winBidding.setBidUrl(recordBiddingNotice.getNoticeDetailUrl());
							}
//							if (checkWinBiddingNotNull(winBidding, noticeFormat)) {
//								winBiddingList.add(winBidding);
//								break;
//							}
							//
						}
						if (checkWinBiddingNotNull(winBidding, noticeFormat)) {
							winBiddingList.add(winBidding);
						}
						if (winBiddingList != null && winBiddingList.size() > 0) {
							if (winBidding.getDealTime() == null) {
								winBidding.setDealTime(DateUtils.dateToString(DateUtils.stringToDate(pubdate, DateUtils.dateformat8), DateUtils.dateformat8));
								winBiddingList.add(winBidding);
							}
							resolveHtml = "2";
							System.out.println(JSONObject.toJSON(winBiddingList));
							saveBiddingListData(winBiddingList, recordBiddingNotice);
						} else {
							resolveHtml = "-1";
//							Date pubdateFormat = DateUtils.stringToDate(pubdate, DateUtils.dateformat8);
//							winBiddingList.add(winBidding);
							
							BiddingListDTO bidding = new BiddingListDTO(winBidding, recordBiddingNotice);
							bidding.setNoticeHtml(noticeHtmlContent);
							
							System.out.println(JSONObject.toJSON(winBiddingList.add(bidding)));
							System.out.println(JSONObject.toJSON(winBiddingList));
							saveBiddingListData(winBiddingList, recordBiddingNotice);
						}
					}
				}
			}
		}
		
		System.err.println("已解析完公告数据，先休眠1分钟。。。");
		sleepTime(60000L);

		System.err.println("======开始 过滤重复存量公告数据===========");
		//过滤重复公告数据，保存到公告存量表
		filterRepeatResolveBiddingToSaveBiddingStock(siteName);
		System.err.println("======结束 过滤重复存量公告数据===========");
	}
	
	/**
	 * 创建公告格式1数据
	 * @param winBidding
	 * @param targetContent
	 * @param element
	 * @param elements
	 */
	String suppliers = "";
	String bidPrices = "";
	StringBuffer supplierBuffer = new StringBuffer();
	StringBuffer bidPriceBuffer = new StringBuffer();
	private void createNoticeFormat1(BiddingListDTO winBidding, String targetContent, Element element, Elements elements) {
		String customer = "";
		String prjId = "";
		String prjName = "";
		String budget = "";
		String bidPrice = "";
		String buyType = "";
		String supplier = "";
		
		//客户（发标、采购方）
		customer = StringUtils.regexData(targetContent, "受 [\\s\\S]+的委托");
		if (StringUtils.isNullOrEmpty(customer)) {
			customer = StringUtils.regexData(targetContent, "受[\\s\\S]+的委托");
			if (StringUtils.isNullOrEmpty(customer)) {
				customer = StringUtils.regexData(targetContent, "受[\\s\\S]+委托");
				customer = customer.replace("委托", "");
			} else {
				customer = customer.replace("的委托", "");
			}
		}
		customer = customer.replace("受", "").replace("受 ", "");
		customer = customer.replace(" ", "");
		if (!StringUtils.isNullOrEmpty(customer) && StringUtils.isNullOrEmpty(winBidding.getCustomer())) {
			winBidding.setCustomer(customer);
			
			//地市
			if (!StringUtils.isNullOrEmpty(customer) && StringUtils.isNullOrEmpty(winBidding.getCity())) {
				String city = matchCityByCustomer(customer);
				if (!StringUtils.isNullOrEmpty(city)) {
					winBidding.setCity(city);
				}
			}
		}
		
		//项目ID
		if (targetContent.contains("采购项目编号：") || targetContent.contains("采购项目编号 ") || targetContent.contains("项目编号：")) {
			prjId = StringUtils.regexData(targetContent, "采购项目编号 [a-zA-Z0-9-（）]{12,100}+");
			if (StringUtils.isNullOrEmpty(prjId)) {
				prjId = StringUtils.regexData(targetContent, "采购项目编号：[a-zA-Z0-9-（）]{12,100}+");
			}
			prjId = prjId.replace("采购项目编号：", "").replace("采购项目编号 ", "");
			if (StringUtils.isNullOrEmpty(prjId)) {
				targetContent = targetContent.replaceAll("\\s+", "");
				prjId = StringUtils.regexData(targetContent, "[a-zA-Z0-9-]{12,100}+");
				prjId = prjId.replace("采购项目编号：", "");
			}
			if (StringUtils.isNullOrEmpty(prjId)) {
				prjId = StringUtils.regexData(targetContent, "项目编号： [a-zA-Z0-9-（）]{12,100}+");
				prjId = prjId.replace("项目编号：", "");
				if (StringUtils.isNullOrEmpty(prjId)) {
					prjId = StringUtils.regexData(targetContent, "项目编号：[a-zA-Z0-9]{6,100}+");
					prjId = prjId.replace("项目编号：", "");
				}
			}
			if (!StringUtils.isNullOrEmpty(prjId) && StringUtils.isNullOrEmpty(winBidding.getPrjId())) {
				winBidding.setPrjId(prjId);
			}
		}
		
		//项目名称
		prjName = StringUtils.regexData(targetContent, "采购项目名称 [\\s\\S]+");
		if (StringUtils.isNullOrEmpty(prjName)) {
			prjName = StringUtils.regexData(targetContent, "采购项目名称：[\\s\\S]+");
			prjName = prjName.replace("采购项目名称：", "");
		}
		if (StringUtils.isNullOrEmpty(prjName)) {
			prjName = StringUtils.regexData(targetContent, "项目名称：[\\s\\S]+");
			prjName = prjName.replace("项目名称：", "");
		}
		if (!StringUtils.isNullOrEmpty(prjName) && StringUtils.isNullOrEmpty(winBidding.getPrjName())) {
			winBidding.setPrjName(prjName);
		}

		//项目预算金额
		if (targetContent.contains("采购项目预算金额（元）：") || targetContent.contains("采购项目预算金额：")
				|| targetContent.contains("项目预算总价：") || targetContent.contains("预算金额:")) {
			targetContent = targetContent.replace(",", "").replace("3.", "");
			if (targetContent.indexOf("、") != -1) {
				targetContent = targetContent.split("、")[1];
			}
			budget = StringUtils.regexData(targetContent, "采购项目预算金额（元）：([1-9]\\d*\\.?\\d*)|(0\\.\\d*[1-9])");
			if (StringUtils.isNullOrEmpty(budget)) {
				budget = StringUtils.regexData(targetContent, "([1-9]\\d*\\.?\\d*)|(0\\.\\d*[1-9])");
			}
			budget = budget.replace("采购项目预算金额（元）：", "").replace("预算金额:", "");
			if (targetContent.contains("万元") || targetContent.contains("万")) {
				budget+= "万";
			}
			budget = budget.replace("采购项目预算金额（元）：", "").replace("采购项目预算金额：", "").replace("项目预算总价：", "");
			if (!StringUtils.isNullOrEmpty(budget) && StringUtils.isNullOrEmpty(winBidding.getBudget())) {
				winBidding.setBudget(budget);
			}
		}
		
		//采购方式
		buyType = StringUtils.regexData(targetContent, "采购方式：[\\s\\S]+");
		buyType = buyType.replace("采购方式：", "");
		if (!StringUtils.isNullOrEmpty(buyType) && StringUtils.isNullOrEmpty(winBidding.getBuyType())) {
			winBidding.setBuyType(buyType);
		}
		
		//获取中标供应商
		if ((targetContent.contains("中标供应商名称：") || targetContent.contains("中标单位：") || targetContent.contains("中标人名称：")
				|| targetContent.contains("中标标的名称："))
				&& StringUtils.isNullOrEmpty(winBidding.getSupplier())) {
			supplier = targetContent.replace("1.", "").replace("1、", "").replace("2.", "").replace("（一）" ,"")
					.replace("中标供应商名称：", "").replace("中标单位：", "").replace("中标人名称：", "").replace("中标标的名称：", "");
			if (!StringUtils.isNullOrEmpty(supplier) && StringUtils.isNullOrEmpty(winBidding.getSupplier())) {
				winBidding.setSupplier(supplier);
			}
		}
		//获取中标金额
		if (targetContent.contains("中标金额：") && StringUtils.isNullOrEmpty(winBidding.getBidPrice())) {
			if (targetContent.indexOf("：") != -1) {
				try {
					if (targetContent.split("：").length > 1) {
						targetContent = targetContent.split("：")[1];
						targetContent = targetContent.replace(" ", "");
					}
				} catch (Exception e) {
					System.out.println("截取异常：" + targetContent);
					e.printStackTrace();
					System.exit(0);
				}
			}
			bidPrice = targetContent.replace("中标金额：", "");
			bidPrice = StringUtils.regexData(targetContent, "([1-9]\\d*\\.?\\d*)|(0\\.\\d*[1-9])");
			if (StringUtils.isNullOrEmpty(bidPrice)) {
				bidPrice = StringUtils.regexData(targetContent, "中标金额：([1-9]\\d*\\.?\\d*)|(0\\.\\d*[1-9])");
			}
			if (targetContent.contains("万元") || targetContent.contains("万")) {
				bidPrice+="万";
			}
			if (!StringUtils.isNullOrEmpty(bidPrice)  && StringUtils.isNullOrEmpty(winBidding.getBidPrice())) {
				winBidding.setBidPrice(bidPrice);
			}
		}
		if ((targetContent.contains("中标供应商") || targetContent.contains("中标供应商名称") || targetContent.contains("成交供应商名称："))
				&& StringUtils.isNullOrEmpty(winBidding.getSupplier())) {
			Boolean existHtml = true;
			String supplierText = element.parent().select("#generalArticleEditForm00080101_bidOrgDetailTDP > p").html();
			if (StringUtils.isNullOrEmpty(supplierText)) {
				supplierText = element.parent().select("#generalArticleEditForm000805_bidOrgDetailTDP > p").html();
				if (StringUtils.isNullOrEmpty(supplierText)) {
					supplierText = element.parent().select("#generalArticleEditForm0008gz_bidOrgDetailTDP > p").html();
				}
			}
			if (StringUtils.isNullOrEmpty(supplierText)) {
				supplierText = element.parent().select("span").text();
				existHtml = false;
			}
			if (existHtml) {
				supplierText = supplierText.replaceAll("\\s*", "").replace("&nbsp;", "");
				supplier = StringUtils.regexData(supplierText, "中标供应商名称<u>[\\s\\S]+</u>法人代表");
				String supplierMore = StringUtils.regexData(supplierText, "中标供应商名称");
				String[] supplierTexts = supplierMore.split(",");
				if (supplierTexts != null && supplierTexts.length > 1) {
					String[] suppliers = null;//StringUtils.dealSuppliers(supplier).split(",");
					if (suppliers != null && suppliers.length > 1) {
						winBidding.setSuppliers(suppliers);
					}
				} else {
					supplier = supplier.replace("中标供应商名称<u>", "");
					supplier = supplier.replace("</u>法人代表", "").replace("；", "");
					if (!StringUtils.isNullOrEmpty(supplier) && StringUtils.isNullOrEmpty(winBidding.getSupplier())) {
						winBidding.setSupplier(supplier);
					}
				}
				
				//获取中标金额
				if (StringUtils.isNullOrEmpty(winBidding.getBidPrice())) {
					String bidPriceTdText = "";
					try {
						bidPriceTdText = element.parent().select("table > tbody > tr").get(0).select("td").last().text();
					} catch (Exception e) {}
					if ("中标、成交金额（元）".equals(bidPriceTdText) || "中标金额（元）".equals(bidPriceTdText)
							|| "中标金额".equals(bidPriceTdText)
							|| "中标、成交下浮率（百分比）".equals(bidPriceTdText) || "中标、成交金额".equals(bidPriceTdText)) {
						if (winBidding.getSuppliers() != null && winBidding.getSuppliers().length > 1) {
							int index = 1;
							StringBuffer bidPriceBuffer = new StringBuffer();
							String bidPriceText = element.parent().select("table > tbody > tr").get(index).select("td").last().text();
							if (bidPriceText.indexOf("；") != -1) {
								try {
									for (int i = 0; i < winBidding.getSuppliers().length; i++) {
										String bidPriceTextString = bidPriceText.split("；")[i];
										if (!StringUtils.isNullOrEmpty(bidPriceTextString)) {
											bidPriceBuffer.append(bidPriceTextString);
											if (i != (winBidding.getSuppliers().length - 1)) {
												bidPriceBuffer.append(",");
											}
										} else {
											if (StringUtils.isNullOrEmpty(winBidding.getBidPrice()) && !StringUtils.isNullOrEmpty(bidPriceBuffer.toString())) {
												winBidding.setBidPrice(bidPriceBuffer.toString().split(",")[0]);
											}
											break;
										}
									}
								} catch (Exception e) {
									e.printStackTrace();
								}
							} else {
								for (int i = 0; i < winBidding.getSuppliers().length; i++) {
									try {
										bidPriceText = element.parent().select("table > tbody > tr").get(index).select("td").last().text();
										bidPriceText = bidPriceText.replace(",", "").replaceAll("\\s*", "").replace(" ", "").replace("   ", "").replace("/", "");
										if (!StringUtils.isNullOrEmpty(bidPriceText)) {
											bidPriceBuffer.append(bidPriceText);
											if (i != (winBidding.getSuppliers().length - 1)) {
												bidPriceBuffer.append(",");
											}
										} else {
											if (StringUtils.isNullOrEmpty(winBidding.getBidPrice()) && !StringUtils.isNullOrEmpty(bidPriceBuffer.toString())) {
												winBidding.setBidPrice(bidPriceBuffer.toString().split(",")[0]);
											}
											break;
										}
										index++;
									} catch (Exception e) {}
								}
							}
							String bidPriceResult = bidPriceBuffer.toString();
							if (!StringUtils.isNullOrEmpty(bidPriceResult) && bidPriceResult.split(",").length > 1) {
								winBidding.setBidPrices(bidPriceResult.split(","));
							}
						} else {
							bidPrice = element.parent().select("table > tbody > tr").get(1).select("td").last().text();
							bidPrice = bidPrice.replace(",", "");
							bidPrice = bidPrice.replace("中标金额：", "");
							bidPrice = bidPrice.replace("￥", "");
						}
						if (bidPrice.contains("万元") || bidPrice.contains("万")) {
							bidPrice+="万";
						}
						if (!StringUtils.isNullOrEmpty(bidPrice)  && StringUtils.isNullOrEmpty(winBidding.getBidPrice())) {
							winBidding.setBidPrice(bidPrice);
						}
					}
				}
			} else {
				if (winBidding.getSuppliers() != null && winBidding.getSuppliers().length > 1) {
					noticeFormat = 3;
				}
				if ((winBidding.getSuppliers() == null || winBidding.getSuppliers().length == 0) && noticeFormat == 3) {
					Elements spanElements = elements;
					ListIterator<Element> resultIter = spanElements.listIterator();
					//重新遍历html元素中所有供应商以及中标金额
					while(resultIter.hasNext()) {
						Element get = resultIter.next();
						targetContent = get.text();
						if (targetContent.contains("中标供应商名称：")) {
							if (StringUtils.isNullOrEmpty(winBidding.getSupplier())) {
								supplier = StringUtils.regexData(targetContent, "中标供应商名称：[\\s\\S]+");
								supplier = supplier.replace("中标供应商名称：", "").replace("；", "");
								if (!StringUtils.isNullOrEmpty(supplier)) {
									supplierBuffer.append(supplier).append(",");
									winBidding.setSupplier(supplier);
									suppliers = supplierBuffer.toString();
									winBidding.setSuppliers(suppliers.split(","));
								}
							} else {
								supplier = StringUtils.regexData(targetContent, "中标供应商名称：[\\s\\S]+");
								supplier = supplier.replace("中标供应商名称：", "").replace("；", "");
								if (!StringUtils.isNullOrEmpty(supplier)) {
									//第一个供应商名称不与第二个供应商相同
									supplierBuffer.append(supplier).append(",");
									suppliers = supplierBuffer.toString();
									if (suppliers.lastIndexOf(",") != -1) {
										suppliers = suppliers.substring(0, suppliers.lastIndexOf(","));
									}
									winBidding.setSuppliers(suppliers.split(","));
									if (supplierBuffer.toString().split(",").length > 1) {
										if (supplierBuffer.toString().split(",")[0].equals(supplierBuffer.toString().split(",")[1])) {
											noticeFormat = 1;
										}
									}
								}
							}
						}
						if (targetContent.contains("中标金额：") || targetContent.contains("中标金额")) {
							if (StringUtils.isNullOrEmpty(winBidding.getBidPrice())) {
								targetContent = targetContent.replace(",", "");
								bidPrice = StringUtils.regexData(targetContent, "中标金额：[\\s\\S]+");
								bidPrice = bidPrice.replace("中标金额：" , "");
								bidPrice = bidPrice.replace("人民币", "");
								if (!StringUtils.isNullOrEmpty(bidPrice)) {
									bidPriceBuffer.append(bidPrice).append(",");
									winBidding.setBidPrice(bidPrice);
									winBidding.setBidPrices(bidPriceBuffer.toString().split(","));
								}
							} else {
								targetContent = targetContent.replace(",", "");
								bidPrice = StringUtils.regexData(targetContent, "中标金额：[\\s\\S]+");
								bidPrice = bidPrice.replace("中标金额：", "");
								bidPrice = bidPrice.replace("人民币", "");
								if (!StringUtils.isNullOrEmpty(bidPrice)) {
									bidPriceBuffer.append(bidPrice).append(",");
									bidPrices = bidPriceBuffer.toString();
									if (bidPrices.lastIndexOf(",") != -1) {
										bidPrices = bidPrices.substring(0, bidPrices.lastIndexOf(","));
									}
									winBidding.setBidPrices(bidPrices.split(","));
								}
							}
						}
					}
				}
			}
		}
		
		//中标日期
		if (targetContent.contains("评审日期：") || targetContent.contains("中标日期：")) {
			String dealTimeStr = StringUtils.regexData(targetContent, "\\d{4}-\\d{1,2}-\\d{1,2}");
			if (StringUtils.isNullOrEmpty(dealTimeStr)) {
				dealTimeStr = StringUtils.regexData(targetContent, "\\d{4}年\\d{1,2}月\\d{1,2}日");
				dealTimeStr = dealTimeStr.replace("年", "-").replace("月", "-").replace("日", "");
			}
			if (!StringUtils.isNullOrEmpty(dealTimeStr) && winBidding.getDealTime() == null) {
				String dateDetailStr = dealTimeStr;
//				if (dealTimeStr.indexOf("-") != -1) {
//					dateDetailStr = DateUtils.dateToStringFormat(dealTimeStr, DateUtils.dateformat8, DateUtils.dateformat14);
//				}
				Date dealTime = DateUtils.stringToDate(dateDetailStr, DateUtils.dateformat8);
				String dealTimeString = DateUtils.dateToString(dealTime, DateUtils.dateformat8);
				winBidding.setDealTime(dealTimeString);
			}
		}
	}
	
	/**
	 * 创建公告格式2数据
	 * @param winBidding
	 * @param targetContent
	 * @param element
	 */
	private void createNoticeFormat2(BiddingListDTO winBidding, String targetContent, Element element) {
		String prjId = "";
		String prjName = "";
		String buyType = "";
		String supplier = "";
		String bidPrice = "";
		
		//项目ID
		prjId = StringUtils.regexData(targetContent, "项目编号：[a-zA-Z0-9-]{12,100}+");
		if (StringUtils.isNullOrEmpty(prjId)) {
			prjId = StringUtils.regexData(targetContent, "项目编号：[a-zA-Z0-9]+");
			if (StringUtils.isNullOrEmpty(prjId)) {
				prjId = StringUtils.regexData(targetContent, "项目编号：[0-9]+");
			}
		}
		prjId = prjId.replaceAll("\\s+", "");
		prjId = prjId.replace("采购项目编号：", "").replace("采购项目编号 ", "").replace("项目编号：", "");
		if (StringUtils.isNullOrEmpty(prjId)) {
			targetContent = targetContent.replaceAll("\\s+", "");
			prjId = StringUtils.regexData(targetContent, "[0-9]+");
		}
		if (!StringUtils.isNullOrEmpty(prjId) && StringUtils.isNullOrEmpty(winBidding.getPrjId())) {
			winBidding.setPrjId(prjId);
		}
		
		//项目名称
		prjName = StringUtils.regexData(targetContent, "项目名称：[\\s\\S]+");
		prjName = prjName.replaceAll("\\s+", "").replace("项目名称：", "");
		if (!StringUtils.isNullOrEmpty(prjName) && StringUtils.isNullOrEmpty(winBidding.getPrjName())) {
			winBidding.setPrjName(prjName);
		}

		//采购方式
		buyType = StringUtils.regexData(targetContent, "发包类型：[\\s\\S]+");
		buyType = buyType.replace("发包类型：", "");
		if (!StringUtils.isNullOrEmpty(buyType) && StringUtils.isNullOrEmpty(winBidding.getBuyType())) {
			winBidding.setBuyType(buyType);
		}
		
		//获取中标供应商
		supplier = StringUtils.regexData(targetContent, "中标单位名称：[\\s\\S]+");
		supplier = supplier.replace("中标单位名称：", "");
		if (!StringUtils.isNullOrEmpty(supplier) && StringUtils.isNullOrEmpty(winBidding.getSupplier())) {
			winBidding.setSupplier(supplier);
		}
			
		//获取中标金额
		if (StringUtils.isNullOrEmpty(winBidding.getBidPrice())) {
			if (targetContent.contains("中标价（万元）：")) {
				bidPrice = targetContent.replace("中标价（万元）：", "");
				if (!StringUtils.isNullOrEmpty(bidPrice)  && StringUtils.isNullOrEmpty(winBidding.getBidPrice())) {
					winBidding.setBidPrice(bidPrice + "万元");
				}
			}
		}
		
		//中标日期
		if (targetContent.contains("中标时间：")) {
			String dealTimeStr = StringUtils.regexData(targetContent, "\\d{4}-\\d{1,2}-\\d{1,2}");
			if (StringUtils.isNullOrEmpty(dealTimeStr)) {
				dealTimeStr = StringUtils.regexData(targetContent, "\\d{4}年\\d{1,2}月\\d{1,2}日");
				dealTimeStr = dealTimeStr.replace("年", "-").replace("月", "-").replace("日", "");
			}
			if (!StringUtils.isNullOrEmpty(dealTimeStr) && winBidding.getDealTime() == null) {
				String dateDetailStr = dealTimeStr;
//				if (dealTimeStr.indexOf("-") != -1) {
//					dateDetailStr = DateUtils.dateToStringFormat(dealTimeStr, DateUtils.dateformat8, DateUtils.dateformat14);
//				}
				Date dealTime = DateUtils.stringToDate(dateDetailStr, DateUtils.dateformat8);
				String dealTimeString = DateUtils.dateToString(dealTime, DateUtils.dateformat8);
				winBidding.setDealTime(dealTimeString);
			}
		}
	}
	
	/**
	 * 从客户（发标、采购方）匹配地市，匹配不上返回-1
	 * @param customer
	 * @return
	 */
	private String matchCityByCustomer(String customer) {
		String city = "-1";
		String[] cityNames = Constants.CITY_NAMES;
		for (String cityName : cityNames) {
			if (customer.contains(cityName)) {
				city = cityName;
				break;
			}
		}
		return city;
	}
	
	/**
	 * 验证中标字段不为空
	 * @param bidding
	 * @return
	 */
	private Boolean checkWinBiddingNotNull(BiddingListDTO winBidding, Integer noticeFormat) {
		if (noticeFormat == 1) {
			if (!StringUtils.isNullOrEmpty(winBidding.getPrjId()) && !StringUtils.isNullOrEmpty(winBidding.getPrjName())
					&& !StringUtils.isNullOrEmpty(winBidding.getBuyType()) && !StringUtils.isNullOrEmpty(winBidding.getCustomer())
					&& !StringUtils.isNullOrEmpty(winBidding.getSupplier()) && winBidding.getDealTime() != null
					&& !StringUtils.isNullOrEmpty(winBidding.getBudget()) && !StringUtils.isNullOrEmpty(winBidding.getBidPrice())
					&& !StringUtils.isNullOrEmpty(winBidding.getBidUrl())) {
				System.out.println("公告格式1，check==>" + JSONObject.toJSONString(winBidding));
				return true;
			} else if (!StringUtils.isNullOrEmpty(winBidding.getPrjId()) && !StringUtils.isNullOrEmpty(winBidding.getPrjName())
					&& !StringUtils.isNullOrEmpty(winBidding.getCustomer()) && winBidding.getDealTime() != null
					&& !StringUtils.isNullOrEmpty(winBidding.getBidUrl())) {
				String supplier = winBidding.getSupplier();
				String buyType = winBidding.getBuyType();
				String budget = winBidding.getBudget();
				String bidPrice = winBidding.getBidPrice();
				winBidding.setSupplier(StringUtils.isNullOrEmpty(supplier) ? "-1" : supplier);
				winBidding.setBuyType(buyType);
				winBidding.setBudget(StringUtils.isNullOrEmpty(budget) ? "0.0" : budget);
				winBidding.setBidPrice(StringUtils.isNullOrEmpty(bidPrice) ? "0.0" : bidPrice);
				System.out.println("公告格式1，check==>" + JSONObject.toJSONString(winBidding));
				return true;
			}
		} else if (noticeFormat == 2) {
			if (!StringUtils.isNullOrEmpty(winBidding.getPrjId()) && !StringUtils.isNullOrEmpty(winBidding.getPrjName())
					&& !StringUtils.isNullOrEmpty(winBidding.getBuyType())
					&& !StringUtils.isNullOrEmpty(winBidding.getSupplier()) && winBidding.getDealTime() != null
					&& !StringUtils.isNullOrEmpty(winBidding.getBidPrice()) && !StringUtils.isNullOrEmpty(winBidding.getBidUrl())) {
				winBidding.setCustomer("-1");
				winBidding.setBudget("0.0");
				System.out.println("公告格式2，check==>" + JSONObject.toJSONString(winBidding));
				return true;
			}
		} else if (noticeFormat == 3) {
			//存在多个供应商
			if (!StringUtils.isNullOrEmpty(winBidding.getPrjId()) && !StringUtils.isNullOrEmpty(winBidding.getPrjName())
					&& !StringUtils.isNullOrEmpty(winBidding.getBuyType()) && winBidding.getDealTime() != null
					&& (winBidding.getSuppliers() != null && winBidding.getSuppliers().length > 1)
					&& !StringUtils.isNullOrEmpty(winBidding.getBidPrice()) && !StringUtils.isNullOrEmpty(winBidding.getBidUrl())) {
				System.out.println("公告格式3，check==>" + JSONObject.toJSONString(winBidding));
				return true;
			}
		}
		return false;
	}
	
	/**
	 * 保存解析html数据处理结果
	 * @param winBidding
	 * @param recordNotice
	 * @return
	 */
	private Boolean saveBiddingStockRepeat(BiddingListDTO bidding, RecordBiddingNoticeDTO recordNotice) {
		Boolean saveFlag = false;
		try {
			bidding.setCity(bidding.matchCityByName(bidding.getCity(), bidding));
			bidding.setManual(bidding.checkManual(bidding));
			this.bidService.saveBiddingStockRepeat(bidding);
			saveFlag = true;
		} catch (Exception e) {
			try {
				this.bidService.saveBiddingStockRepeat(new BiddingListDTO(bidding, recordNotice));
				saveFlag = true;
			} catch (Exception e1) {
				saveFlag = false;
				System.out.println("保存解析数据出现异常：" + StringUtils.getErrorInfoFromException(e1));
			}
		}
		noticeFormat = 1;
		return saveFlag;
	}
	
	/**
	 * 保存中标人数据
	 * @param winBiddingList
	 * @param recordBiddingNotice
	 */
	private void saveBiddingListData(List<BiddingListDTO> winBiddingList, RecordBiddingNoticeDTO recordBiddingNotice) throws Exception {
		Boolean saveFlag = false;
		Boolean isRepeat = false;
		if(winBiddingList != null && winBiddingList.size() > 0) {
			System.out.println();
			System.out.println("size：" + winBiddingList.size());
			System.out.println(JSONObject.toJSONString(winBiddingList));
			
			for(BiddingListDTO winBidding : winBiddingList) {
				try {
					if (winBidding.getSuppliers() != null && winBidding.getBidPrices() != null) {
						if (winBidding.getSuppliers().length > 1 && winBidding.getBidPrices().length > 1) {
							if (winBidding.getSuppliers().length == winBidding.getBidPrices().length) {
								for (int i = 0; i < winBidding.getSuppliers().length; i++) {
									String supplier = winBidding.getSuppliers()[i];
									String bidPrice = winBidding.getBidPrices()[i];
									winBidding.setSupplier(supplier);
									winBidding.setBidPrice(bidPrice);
									if (StringUtils.isNullOrEmpty(winBidding.getBudget())) {
										winBidding.setBudget("0.0");
									}
									
									Integer countResolveHtml = this.bidService.countResolveHtml(new BiddingListQuery(winBidding));
									if (countResolveHtml == 0) {
										saveFlag = saveBiddingStockRepeat(winBidding, recordBiddingNotice);
									} else {
										isRepeat = true;
										System.out.println("已解析：" + winBidding);
									}
									
									if (saveFlag) {
										this.bidService.updateRecordBiddingNotice(resolveHtml, recordBiddingNotice.getId());
										supplierBuffer = new StringBuffer();
										bidPriceBuffer = new StringBuffer();
										noticeFormat = 1;
									} else {
										if (isRepeat) {
											this.bidService.updateRecordBiddingNotice(BiddingNoticeOperationMode.RESOLVE_HTML_REPEAT.toString(), recordBiddingNotice.getId());
											supplierBuffer = new StringBuffer();
											bidPriceBuffer = new StringBuffer();
											noticeFormat = 1;
										}
									}
								}
							} else {
								if (StringUtils.isNullOrEmpty(winBidding.getBudget())) {
									winBidding.setBudget("0.0");
								}
								Integer countResolveHtml = this.bidService.countResolveHtml(new BiddingListQuery(winBidding));
								if (countResolveHtml == 0) {
									saveFlag = saveBiddingStockRepeat(winBidding, recordBiddingNotice);
								} else {
									isRepeat = true;
									System.out.println("已解析：" + winBidding);
								}
							}
							
							if (saveFlag) {
								this.bidService.updateRecordBiddingNotice(resolveHtml, recordBiddingNotice.getId());
								supplierBuffer = new StringBuffer();
								bidPriceBuffer = new StringBuffer();
								noticeFormat = 1;
							} else {
								if (isRepeat) {
									this.bidService.updateRecordBiddingNotice(BiddingNoticeOperationMode.RESOLVE_HTML_REPEAT.toString(), recordBiddingNotice.getId());
									supplierBuffer = new StringBuffer();
									bidPriceBuffer = new StringBuffer();
									noticeFormat = 1;
								}
							}
						} else {
							if (StringUtils.isNullOrEmpty(winBidding.getBudget())) {
								winBidding.setBudget("0.0");
							}
							Integer countResolveHtml = this.bidService.countResolveHtml(new BiddingListQuery(winBidding));
							if (countResolveHtml == 0) {
								saveFlag = saveBiddingStockRepeat(winBidding, recordBiddingNotice);
							} else {
								isRepeat = true;
								System.out.println("已解析：" + winBidding);
							}
						}
					} else {
						if (StringUtils.isNullOrEmpty(winBidding.getBudget())) {
							winBidding.setBudget("0.0");
						}
						Integer countResolveHtml = this.bidService.countResolveHtml(new BiddingListQuery(winBidding));
						if (countResolveHtml == 0) {
							saveFlag = saveBiddingStockRepeat(winBidding, recordBiddingNotice);
						} else {
							isRepeat = true;
							System.out.println("已解析：" + winBidding);
						}
					}
					
					if (saveFlag) {
						this.bidService.updateRecordBiddingNotice(resolveHtml, recordBiddingNotice.getId());
						supplierBuffer = new StringBuffer();
						bidPriceBuffer = new StringBuffer();
						noticeFormat = 1;
					} else {
						if (isRepeat) {
							this.bidService.updateRecordBiddingNotice(BiddingNoticeOperationMode.RESOLVE_HTML_REPEAT.toString(), recordBiddingNotice.getId());
							supplierBuffer = new StringBuffer();
							bidPriceBuffer = new StringBuffer();
							noticeFormat = 1;
						}
					}
				} catch (Exception e) {
					e.printStackTrace();
					saveFlag = false;
				}
			}
		}
	}
	
	String proxyIp = Constants.PROXY_IP;
	int proxyPort = Constants.PROXY_PORT;
	private String getHtmlContent(String bidUrl) {
		String result = "";
		Long sleepTime1 = 1000L;
		try {
			sleepTime();
//			proxyIp = "115.29.200.195";
//			proxyPort = 808;
			io.github.biezhi.elves.ok.http.Response okHttpResponse;
			if(!Constants.IS_PROXY){
				okHttpResponse = FastHttpClient
						.get()
						.addHeader("cookie", "bidguid=af8e78fb-cd3c-427a-9d2f-28befaf4168d; UM_distinctid=164bcf3d55133-07adf15334f854-6114167a-1fa400-164bcf3d55341; _uab_collina=153387131164706605514218; isshowtcc=isshowtcc; BIDCTER_USERNAME=UserName=13660447695; keywords==%e9%9b%aa%e4%ba%ae%e5%b7%a5%e7%a8%8b; _umdata=486B7B12C6AA95F2A4F682D6C31223F0986ECB48B6B5E1D6BE7EA5816E9CFF9D1E467C2930EA264FCD43AD3E795C914C5D97F0FEE136D0563C9C341A87C6C677; ASP.NET_SessionId=nw2yggonxpe4wmh3aqfhfrbe; Hm_lvt_9954aa2d605277c3e24cb76809e2f856=1535006591,1535767259,1535771112,1536026669; Hm_lpvt_9954aa2d605277c3e24cb76809e2f856=1536026669; aspcn=id=3021574&name=13660447695&vip=2&company=%e5%a4%a9%e8%ae%af%e7%91%9e%e8%be%be%e9%80%9a%e4%bf%a1%e6%8a%80%e6%9c%af%e6%9c%89%e9%99%90%e5%85%ac%e5%8f%b8&lianxiren=%e8%ae%b8&tel=&email=&diqu=&Token=E65B83029257040ACDD5863EC5B9D3794550D5197CE30A57AA4448E0D6CFCF5FA420115C8B512467DA3C161F486C31EF; PASSKEY=Token=E65B83029257040ACDD5863EC5B9D3794550D5197CE30A57AA4448E0D6CFCF5FA420115C8B512467DA3C161F486C31EF; CNZZDATA888048=cnzz_eid%3D724129228-1532177090-null%26ntime%3D1536029296")
						.url(bidUrl)
						.build()
						.execute();
			}else{
//				if(("132.122.1.4".equals(proxyIp) && proxyPort == 9898)
//						|| (StringUtils.isNullOrEmpty(proxyIp) && proxyPort == 0)) {
//					resetProxyIp();
//				}
				Proxy proxy = new Proxy(Proxy.Type.HTTP,new InetSocketAddress(proxyIp, proxyPort));
				okHttpResponse = FastHttpClient
						.newBuilder()
						.proxy(proxy)
						.build()
						.get()
						.addHeader("cookie", "bidguid=af8e78fb-cd3c-427a-9d2f-28befaf4168d; UM_distinctid=164bcf3d55133-07adf15334f854-6114167a-1fa400-164bcf3d55341; _uab_collina=153387131164706605514218; isshowtcc=isshowtcc; BIDCTER_USERNAME=UserName=13660447695; keywords==%e9%9b%aa%e4%ba%ae%e5%b7%a5%e7%a8%8b; _umdata=486B7B12C6AA95F2A4F682D6C31223F0986ECB48B6B5E1D6BE7EA5816E9CFF9D1E467C2930EA264FCD43AD3E795C914C5D97F0FEE136D0563C9C341A87C6C677; ASP.NET_SessionId=nw2yggonxpe4wmh3aqfhfrbe; Hm_lvt_9954aa2d605277c3e24cb76809e2f856=1535006591,1535767259,1535771112,1536026669; Hm_lpvt_9954aa2d605277c3e24cb76809e2f856=1536026669; aspcn=id=3021574&name=13660447695&vip=2&company=%e5%a4%a9%e8%ae%af%e7%91%9e%e8%be%be%e9%80%9a%e4%bf%a1%e6%8a%80%e6%9c%af%e6%9c%89%e9%99%90%e5%85%ac%e5%8f%b8&lianxiren=%e8%ae%b8&tel=&email=&diqu=&Token=E65B83029257040ACDD5863EC5B9D3794550D5197CE30A57AA4448E0D6CFCF5FA420115C8B512467DA3C161F486C31EF; PASSKEY=Token=E65B83029257040ACDD5863EC5B9D3794550D5197CE30A57AA4448E0D6CFCF5FA420115C8B512467DA3C161F486C31EF; CNZZDATA888048=cnzz_eid%3D724129228-1532177090-null%26ntime%3D1536029296")
						.url(bidUrl)
						.build()
						.execute();
			}
			result = okHttpResponse.string();
			if(!StringUtils.isNullOrEmpty(result)){
				return result;
			}
		} catch (SocketTimeoutException e) {
			System.err.println("===连接超时，重新调用getHtmlContent()方法===");
			resetProxyIp();
			return getHtmlContent(bidUrl);
		} catch (Exception e) {
			e.printStackTrace();
			System.err.println("===其他异常，重新调用getHtmlContent()方法===");
			resetProxyIp();
			sleepTime(sleepTime1);
			return getHtmlContent(bidUrl);
		}
		return result;
	}
	
	private String postHtmlContent(String keyword, Integer pageIndex) {
		String result = "";
		String bidUrl = "http://my.job592.com/baike/myzb_searchShow.action";
		bidUrl = "http://my.zbyjw.cn/my/myzb_searchShow.action";
		sleepTime();
		try {
			Constants.IS_PROXY = true;
			String contentType = "application/x-www-form-urlencoded; charset=UTF-8";
			String cookie = "JSESSIONID=09E160FC9BCD3D366EED65F5591E9FE7; Hm_lvt_b8e9c1800fce11f23203e99f0b03f9e2=1532232027,1532396307; mk592=CEVuc6_dPp5Ij; rnapl=a443c196e4f5f88b75e3aebb47691e1d; Hm_lpvt_b8e9c1800fce11f23203e99f0b03f9e2=1532484726";
			Map<String, String> paramMap = new HashMap<String, String>();
			paramMap.put("words", "中标");
			paramMap.put("page", pageIndex.toString());
			paramMap.put("locals", "300");
			io.github.biezhi.elves.ok.http.Response okHttpResponse;
			if(!Constants.IS_PROXY){
				okHttpResponse = FastHttpClient
						.post()
						.addHeader("cookie", cookie)
						.addHeader("Content-Type", contentType)
						.addParams(paramMap)
						.url(bidUrl)
						.build()
						.execute();
			}else{
				Proxy proxy = new Proxy(Proxy.Type.HTTP,new InetSocketAddress(proxyIp, proxyPort));
				okHttpResponse = FastHttpClient
						.newBuilder()
						.proxy(proxy)
						.build()
						.post()
						.addHeader("cookie", cookie)
						.addHeader("Content-Type", contentType)
						.addParams(paramMap)
						.url(bidUrl)
						.build()
						.execute();
			}
			result = okHttpResponse.string();
			if(!StringUtils.isNullOrEmpty(result)){
				result = result.replace("<font>", "").replace("<\\/font>", "");
				return result;
			}
		} catch (SocketTimeoutException e) {
			System.err.println("===连接超时，重新调用postHtmlContent()方法===");
			resetProxyIp();
			return postHtmlContent(keyword, pageIndex);
		} catch (Exception e) {
			e.printStackTrace();
			System.err.println("===其他异常，重新调用postHtmlContent()方法===");
			resetProxyIp();
			return postHtmlContent(keyword, pageIndex);
		}
		return result;
	}
	
	private void resetProxyIp() {
		if (Constants.IS_PROXY_IP) {
			try{
				if("132.122.1.4".equals(proxyIp) && proxyPort == 9898) {
					
				}
				Map<String,Object> proxyIpMap = StringUtils.getHttpProxyIp(1);
				String proxyJson = JSONObject.toJSONString(proxyIpMap);
				System.out.println(proxyJson);
				proxyIp = StringUtils.getValueForKey("proxyHost", proxyIpMap);
				proxyPort = StringUtils.getIntValueForKey("proxyPort", proxyIpMap);
				
				System.out.println("代理ip{{}}==>" + proxyIp + "，代理端口{{}}==>" + proxyPort);
			} catch (Exception e) {
				System.err.println("获取第三方代理ip接口异常{{}}==>" + StringUtils.getErrorInfoFromException(e));
			}
		}
	}
	
	private void sleepTime() {
		try {
			Thread.sleep(sleepTime);
			System.err.println("休眠" + sleepTime + "毫秒后，往下执行");
		} catch (Exception e){}
	}

	private void sleepTime(Long sleepTime) {
		try {
			Thread.sleep(sleepTime);
			System.err.println("休眠" + sleepTime + "毫秒后，往下执行");
		} catch (Exception e){}
	}
	
	/**
	 * 处理公告数据中公告标题为空数据
	 * @throws Exception 
	 */
	private void dealBiddingListByNoticeTitleIsNull() throws Exception {
		List<BiddingListDTO> biddings = this.bidService.listBiddingStockImportPage();
		for (BiddingListDTO bidding : biddings) {
			String result = getHtmlContent(bidding.getBidUrl());
			Document doc = Jsoup.parse(result);
			Elements resultElements = null;
			resultElements = doc.select("table > tbody > tr > td > table > tbody > tr > td > h2 > font > #holder");
			if (resultElements == null || resultElements.size() == 0) {
				resultElements = doc.select("table > tbody > tr > td > h2 > font");
			}
			if (resultElements == null || resultElements.size() == 0) {
				resultElements = doc.select("table > tbody > tr > td > blockquote > h2 > font");
			}
//				if (resultElements == null || resultElements.size() == 0) {
//					resultElements = doc.select("table > tbody > tr > td > table").get(1).select("tbody > tr > td > strong");
//				}
			if (resultElements == null || resultElements.size() == 0) {
				try {
					resultElements = doc.select("table > tbody > tr > td > table").get(1).select("tbody > tr > td");
					if (resultElements == null || resultElements.size() == 0) {
						resultElements = doc.select("table > tbody > tr > td > table > tbody > tr > td");
					}
				} catch (Exception e) {
//					if (resultElements == null || resultElements.size() == 0) {
//						resultElements = doc.select("table > tbody > tr > td > table > tbody > tr > td");
//					}
				}
			}
			if (resultElements == null || resultElements.size() == 0) {
				resultElements = doc.select(".main_list > div > table > tbody > tr > td > p");
				if (resultElements == null || resultElements.size() == 0) {
					resultElements = doc.select(".main_list > div > div > table > tbody > tr > td > p");
				}
			}
			
			
			
			if (resultElements == null || resultElements.size() == 0) {
				resultElements = doc.select(".list_content >h5");
			}

			if (resultElements == null || resultElements.size() == 0) {
				resultElements = doc.select(".c_con > .c_swrap > h4");
			}
			
			if (resultElements == null || resultElements.size() == 0) {
				resultElements = doc.select(".main-box > .contentone > .newsTex > .contentttit");
			}
			
			if (resultElements == null || resultElements.size() == 0) {
				resultElements = doc.select(".zw_c_cont > .zw_c_c_title");
			}
			
			if (resultElements == null || resultElements.size() == 0) {
				resultElements = doc.select("#contentFrame > .wrapper > .content > .main > .brief > h3");
			}
			
			
//				System.out.println(doc.toString());
			
			if (resultElements == null || resultElements.size() == 0) {
				resultElements = doc.select(".main > .xx-main > .xx-text > div > div > p > b");
			}
			
			if (resultElements == null || resultElements.size() == 0) {
				resultElements = doc.select(".zcmain > .zcdetail > .content > table > tbody > tr > td > .container > .header > h2");
			}
			
			if (resultElements == null || resultElements.size() == 0) {
				resultElements = doc.select(".main-box > .contentone > .newsTex > .contentttit");
			}
			
			if (resultElements == null || resultElements.size() == 0) {
				resultElements = doc.select(".list_content > h5");
			}
			
			if (resultElements == null || resultElements.size() == 0) {
				resultElements = doc.select(".main > .wrapper > .list_box > .content_box > h1");
			}
			
			if (resultElements == null || resultElements.size() == 0) {
				resultElements = doc.select(".ewb-common-wrap > .ewb-row > .ewb-column1 > div > table > tbody >tr > td > font");
			}
			
			if (resultElements == null || resultElements.size() == 0) {
				resultElements = doc.select("#body_div > #body > div > .noticeTitle > h2");
			}
			
			if (resultElements != null && resultElements.size() > 0) {
				String resultNoticeTitle = resultElements.get(0).text();
				resultNoticeTitle = resultNoticeTitle.replace(" ", "").replace(" ", "");
				this.bidService.updateBiddingStockImport(resultNoticeTitle, bidding.getPrjId());
			}
		}
	}
	
	/**
	 * 处理存量重复公告清单数据
	 * @throws Exception
	 */
	Integer size = 1;
	Integer pageNo = 100;
	private void dealRepeatResolveBiddingStock(String siteName) throws Exception {
		Integer countStockRepeat = this.bidService.countResolveBiddingStockRepeat(siteName);
		if (countStockRepeat > 0) {
			List<ResolveBiddingDTO> resolveBiddings = this.bidService.listResolveBiddingStockRepeatPage(new ResolveBiddingQuery(size, siteName));
			for (ResolveBiddingDTO resolveBidding : resolveBiddings) {
				try {
					BiddingListDTO biddingList = resolveBidding.clone(BiddingListDTO.class);
					//根据项目编号、供应商和中标金额统计过滤数据重复
					Integer countResolveHtml = 0;
					if (!StringUtils.isNullOrEmpty(resolveBidding.getPrjName()) && !StringUtils.isNullOrEmpty(resolveBidding.getSupplier())
							&& !StringUtils.isNullOrEmpty(resolveBidding.getBidPrice())) {
						BiddingListQuery query = new BiddingListQuery(biddingList);
						query.setTableName("t_bidding_stock");
						countResolveHtml = this.bidService.countResolveFilterRepeat(query);
					}
					
//					if ("2018年从化区农村数字电影放映工程项目".equals(resolveBidding.getPrjName())) {
//						System.out.println(countResolveHtml);
//					}
					
					if (countResolveHtml == 0) {
						//新增存量公告清单数据
						this.bidService.saveResolveBiddingStock(resolveBidding);
						resolveBidding.setNoticeHtml("");
						resolveBidding.setNoticeIframeHtml("");
						System.out.println("已插入：" + resolveBidding);
					} else {
						resolveBidding.setNoticeHtml("");
						resolveBidding.setNoticeIframeHtml("");
						System.out.println("已重复：" + resolveBidding);
					}
					Integer saveFlag = countResolveHtml == 0 ? BiddingStockSave.SAVE : BiddingStockSave.REPEAT_SAVE;
					//更新存量重复公告清单数据表保存标识为1
					this.bidService.updateBiddingListStock("t_bidding_stock_repeat", saveFlag, resolveBidding.getId());
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
			
			if (size > 6) {
				size = 1;
			}
			size++;
			dealRepeatResolveBiddingStock(siteName);
		}
		System.err.println("已处理完存量重复数据！！！");
//		sleepTime();
	}
	
	/**
	 * 过滤重复公告数据，保存到公告存量表
	 * @throws Exception
	 */
	private void filterRepeatResolveBiddingToSaveBiddingStock(String siteName) throws Exception {
//		dealBiddingListByNoticeTitleIsNull();
		if (MapConstants.SITE_BY_EN_MAP.containsKey(siteName)) {
			siteName  = StringUtils.getValueForKey(siteName, MapConstants.SITE_BY_EN_MAP);
		}
		dealRepeatResolveBiddingStock(siteName);
		
		System.err.println("已处理完存量数据，先休眠1分钟。。。");
		sleepTime(60000L);
//		System.err.println("不往下处理数据筛选。");
//		System.exit(0);
		
		System.err.println("======开始 处理数据筛选===========");
		listResolveBiddingPage(siteName);
		System.err.println("======结束 处理数据筛选===========");
	}
	
	/**
	 * 处理解析后公告清单数据
	 * @throws Exception
	 */
	private void listResolveBiddingPage(String siteName) throws Exception {
		String tableName = "t_bidding_list";
		if (MapConstants.SITE_BY_EN_MAP.containsKey(siteName)) {
			siteName  = StringUtils.getValueForKey(siteName, MapConstants.SITE_BY_EN_MAP);
		}
		while(true) {
			List<BiddingKeywordDTO> keywords = this.bidService.listBiddingKeyword(1);
			List<ResolveBiddingDTO> resolveBiddings = this.bidService.listResolveBiddingStockPage(new ResolveBiddingQuery(size, siteName));
			if (resolveBiddings == null || resolveBiddings.size() == 0) {
				break;
			}
			for (ResolveBiddingDTO resolveBidding : resolveBiddings) {
				if (StringUtils.isNullOrEmpty(resolveBidding.getPrjName())
						|| StringUtils.isNullOrEmpty(resolveBidding.getNoticeTitle())) {
					continue;
				}
				BiddingListDTO biddingList = new BiddingListDTO();
				
				for (BiddingKeywordDTO biddingKeyword : keywords) {
					String keyword = biddingKeyword.getKeyword();
					if (!resolveBidding.getNoticeTitle().contains(keyword)
							&& (resolveBidding.getNoticeTitle().contains("咨询")
							&& resolveBidding.getNoticeTitle().contains("资格"))) {
						continue;
					} else {
						try {
							if (resolveBidding.getNoticeTitle().contains(keyword)) {
								biddingList = resolveBidding.clone(BiddingListDTO.class);
//								biddingList.setTableName(tableName);
								biddingList.setPrjType(biddingKeyword.getKeywordType());
								biddingList.setMatchKeyword(keyword);
								
								BiddingListQuery query = new BiddingListQuery(biddingList);
								query.setTableName("t_bidding_list");
								
								//根据项目编号、供应商和中标金额统计过滤数据重复
								Integer countResolveHtml = this.bidService.countResolveFilterRepeat(query);
								if (countResolveHtml == 0) {
									//新增处理解析公告清单数据
									this.bidService.saveResolveBiddingList(biddingList);
									biddingList.setNoticeHtml("");
									biddingList.setNoticeIframeHtml("");
									System.out.println("已插入：" + biddingList);
								} else {
									biddingList.setNoticeHtml("");
									biddingList.setNoticeIframeHtml("");
									System.out.println("已重复：" + biddingList);
								}
								
								Integer saveFlag = countResolveHtml == 0 ? BiddingStockSave.SAVE : BiddingStockSave.REPEAT_SAVE;
								//更新存量公告清单数据表保存标识
								this.bidService.updateBiddingListStock("t_bidding_stock", saveFlag, resolveBidding.getId());
								break;
							}
						} catch (Exception e) {
							e.printStackTrace();
						}
					}
				}
			}
			size++;
		}
		System.err.println("已处理筛选完成！！！");
		System.exit(0);
	}
	
	/**
	 * 比较日期，判断数据库中最大发布日期与当前爬取的日期是否相等
	 * 相等则返回true，不等则返回false
	 * @param date				参数说明：要比较的时间
	 * @param targetDate		参数说明：指定时间
	 * @throws ParseException 
	 */
	private Boolean compareDate(String date, String targetDate) throws ParseException {
		String pubdate = targetDate;
		if (StringUtils.isNullOrEmpty(pubdate)) {
			Date currentDay = DateUtils.stringToDate(DateUtils.getTime(-3, DateUtils.dateformat8), DateUtils.dateformat8);
			pubdate = DateUtils.dateToString(currentDay, DateUtils.dateformat8);
		} else {
			Date manualDay = DateUtils.stringToDate(pubdate, DateUtils.dateformat8);
			pubdate = DateUtils.getTime(-3, manualDay, DateUtils.dateformat8);
		}
		if (pubdate.equals(date)) {
			return true;
		}
		return false;
	}
	
	/**
	 * 保存记录采标网公告数据
	 * @param keywordType
	 * @param siteName
	 */
	String cbwUrl = "";
	public void saveRecordCbwNotice(Integer keywordType, String siteName) throws Exception {
		Constants.IS_PROXY = true;
//		String url = "https://search.bidcenter.com.cn/search?diqu=19&type=4&page=" + pageIndex;
		List<BiddingKeywordDTO> keywords = this.bidService.listBiddingKeyword(keywordType);
		if (keywords != null && keywords.size() > 0) {
			for (BiddingKeywordDTO biddingKeyword : keywords) {
				String keyword = biddingKeyword.getKeyword();
				cbwUrl = "https://search.bidcenter.com.cn/search?diqu=19&type=4&page=" + pageIndex + "&keywords=" + keyword;
				Boolean collectionFlag = collectionCbwNotice(keyword, siteName);
				if (collectionFlag) {
					continue;
				}
			}
			keywordType++;
			saveRecordCbwNotice(keywordType, siteName);
		} else {
			System.out.println("不存在此类型的关键字数据，当前要查询关键字类型为：" + keywordType + "，程序自动停止。。。");
			System.exit(0);
		}
	}
	
	/**
	 * 采集采标网公告列表
	 * @param keyword
	 * @param siteName
	 */
	private Boolean collectionCbwNotice(String keyword, String siteName) {
		Boolean collectionFlag = false;
		cbwUrl = "https://search.bidcenter.com.cn/search?diqu=19&type=4&page=" + pageIndex + "&keywords=" + keyword;
		String result = getHtmlContent(cbwUrl);
		
		String targetDate = this.getNewPubdateBySiteName(siteName, keyword);
		if (!StringUtils.isNullOrEmpty(result)) {
			Elements elements = getDocument(result).select("#jq_project_list > tbody > tr");
			if (elements != null && elements.size() > 0) {
				int index = 0;
				for (Element element : elements) {
					try {
						String tdColspan = element.select("td").attr("colspan");
						if (!StringUtils.isNullOrEmpty(tdColspan)) {
							if (index == 0) {
								pageIndex = 1;
								collectionFlag = true;
								continue;
							} else {
								if (collectionFlag) {
									break;
								}
								pageIndex++;
								return collectionCbwNotice(keyword, siteName);
							}
						}
						index++;
						String noticeTitle = element.select("td").get(1).select(".zb_title > a").text();
						String pubdate = element.select("td").get(6).select(".list_time").text();
						String href = element.select("td").get(1).select(".zb_title > a").attr("href");
						String id = getIdByUrl(href);
						
						Boolean compareDateResult = compareDate(pubdate, targetDate);
						if (compareDateResult) {
							//如果发布日期与指定日期相等，则终止程序
//							System.out.println("终止程序，发布日期为：" + pubdate);
//							System.exit(0);
							System.out.println("重新获取下一个关键字，发布日期为：" + pubdate);
							pageIndex = 1;
							collectionFlag = true;
							break;
						} else {
							//创建记录投标公告标题数据对象
							RecordBiddingNoticeDTO recordBiddingNotice = null;
							try {
								BiddingQuery query = new BiddingQuery(id, noticeTitle, href, "中标公告", keyword, pubdate);
								recordBiddingNotice = createRecordBiddingNoticeDTO(query, pageIndex);
								if (recordBiddingNotice != null) {
									System.out.println("要插入的数据：" + recordBiddingNotice);
									this.bidService.saveRecordBiddingNotice(recordBiddingNotice);
								} else {
									continue;
								}
							} catch (Exception e) {
								continue;
							}
						}
					} catch (Exception e) {
						System.err.println("出现异常，链接为：" + cbwUrl + "，");
						System.out.println();
//						System.out.println(StringUtils.getErrorInfoFromException(e));
						collectionFlag = true;
						break;
					}
				}
			}
		}
		return collectionFlag;
	}
	
	/**
	 * 解析采标网公告数据
	 * @param siteName
	 * @throws Exception
	 */
	private void resolveCbwNoticeData(String siteName) throws Exception {
		List<RecordBiddingNoticeDTO> listRecordBiddingNotice = this.bidService.listRecordBiddingNotice(new RecordNoticeQuery(size, Constants.NOTICE_OPERATION_MODE, siteName));
		if (listRecordBiddingNotice == null || listRecordBiddingNotice.size() == 0) {
			System.err.println("====没有要解析的数据");
			try {
				System.err.println("=====开始：过滤重复采标网公告数据，保存到公告存量表=======");
				//过滤重复公告数据，保存到公告存量表
				filterRepeatResolveBiddingToSaveBiddingStock(siteName);
				System.err.println("=====结束：过滤重复采标网公告数据，保存到公告存量表=======");
				
				System.err.println("===开始休眠" + sleepTime + "毫秒......");
				Thread.sleep(sleepTime);
				System.err.println("===结束休眠，重新获取解析数据");
				resolveCbwNoticeData(siteName);
			} catch (Exception e) {}
		}
		if(listRecordBiddingNotice != null && listRecordBiddingNotice.size() > 0) {
			for(RecordBiddingNoticeDTO recordNotice: listRecordBiddingNotice) {
				if (!checkNoticeTitle(recordNotice.getTitle())) {
					continue;
				} else {
					System.out.println("公告标题：" + recordNotice.getTitle() + ",匹配结果：" + checkNoticeTitle(recordNotice.getTitle()) + "，链接：" + recordNotice.getNoticeDetailUrl());
				}
		
				DownloadBiddingHtmlDTO noticeHtml = null;
				try {
					noticeHtml = this.bidService.downloadBiddingHtmlByRecordId(recordNotice.getRecordId());
				} catch (Exception e) {}
				
				if(noticeHtml == null) {
					continue;
				}
				if(noticeHtml != null) {
					String noticeHtmlContent = noticeHtml.getIframeHtmlContent();
					String result = noticeHtmlContent;
					result = result.replace("<i class=\"lxfs_icon\">&nbsp;</i><b>联系方式</b>", "");
//					System.out.println(result);
					
					Document doc = Jsoup.parse(result);
					Elements elements = doc.select("#form1");
					String pubdate = recordNotice.getPubdate();
					if(elements != null && elements.size() > 0) {
						List<BiddingListDTO> resultBiddingList = new ArrayList<BiddingListDTO>();
						BiddingListDTO bidding = createResolveCbwNotice(elements);
						bidding.setRecordId(recordNotice.getRecordId());
						bidding.setNoticeTitle(recordNotice.getTitle());
						bidding.setSpecialName(recordNotice.getKeyword());
						bidding.setPubdate(pubdate);
						bidding.setBidUrl(recordNotice.getNoticeDetailUrl());
						bidding.setSiteName(recordNotice.getSiteName());
						System.out.println(bidding);
						
						bidding.setNoticeHtml(noticeHtmlContent);
						bidding.setNoticeIframeHtml(noticeHtml.getIframeHtmlContent());
						//专项名称
						BiddingKeywordDTO biddingKeyword = this.bidService.getBiddingKeywordByKeyword(recordNotice.getKeyword());
						String specialName = StringUtils.getValueForKey(biddingKeyword.getKeywordType().toString(), SpecialNameMap.SPECIAL_NAME_BY_ID_MAP);
						bidding.setSpecialName(specialName);
						bidding.setManual(bidding.checkManual(bidding));
						if (StringUtils.isNullOrEmpty(bidding.getDealTime())) {
							bidding.setDealTime(DateUtils.dateToString(DateUtils.stringToDate(pubdate, DateUtils.dateformat8), DateUtils.dateformat8));
						}
						resultBiddingList.add(bidding);
						
						if (resultBiddingList != null && resultBiddingList.size() > 0) {
							resolveHtml = "2";
							System.out.println(JSONObject.toJSON(resultBiddingList));
							saveBiddingListData(resultBiddingList, recordNotice);
						} else {
							resolveHtml = "-1";
							
							BiddingListDTO bidding1 = new BiddingListDTO(bidding, recordNotice);
							bidding1.setNoticeHtml(noticeHtmlContent);
							bidding1.setNoticeIframeHtml(noticeHtml.getIframeHtmlContent());
							resultBiddingList.add(bidding1);
							
							System.out.println(JSONObject.toJSON(resultBiddingList));
							saveBiddingListData(resultBiddingList, recordNotice);
						}
					}
				}
			}
		}
		
		System.err.println("已解析完公告数据，先休眠1分钟。。。");
		sleepTime(60000L);
		
		System.err.println("======开始 过滤重复存量公告数据===========");
		//过滤重复公告数据，保存到公告存量表
		filterRepeatResolveBiddingToSaveBiddingStock(siteName);
		System.err.println("======结束 过滤重复存量公告数据===========");
	}
	
	/**
	 * 解析采标网公告处理逻辑
	 * @param elements
	 * @return
	 * @throws Exception
	 */
	private BiddingListDTO createResolveCbwNotice(Elements elements) throws Exception {
		String htmlText = elements.text();
		String htmlCode = elements.html();
		BiddingListDTO resolve = new BiddingListDTO();
		htmlText = htmlText.replace(":", "：");
		if (!StringUtils.isNullOrEmpty(htmlText)) {
			//项目编号
			String prjId = StringUtils.regexData(htmlText, "采购项目编号：[a-zA-Z0-9-（）]{12,100}+");
			if (prjId.indexOf(",") != -1) {
				prjId = prjId.split(",")[0];
			}
			prjId = prjId.replace("采购项目编号：", "");
			resolve.setPrjId(prjId);
			
			//项目名称
			String prjName = StringUtils.regexData(htmlText, "采购项目名称：[\\s\\S]+ 三");
			prjName = prjName.replace("采购项目名称：", "").replace(" 三", "");
			resolve.setPrjName(prjName);
			
			//公告类型
			String noticeName = "中标公告";
			resolve.setNoticeName(noticeName);
			
			//采购方式
			String buyType = StringUtils.regexData(htmlText, "采购方式：[\\s\\S]+ 五");
			buyType = buyType.replace("采购方式：", "").replace(" 五", "");
			resolve.setBuyType(buyType);
			
			//客户（发标、采购方）
			String customer = StringUtils.regexData(htmlText, "受[\\s\\S]+的委托");
			customer = customer.replace("受", "").replace("的委托", "");
			resolve.setCustomer(customer);
			
			//供应商
			String supplierHtmlText = htmlText.replace("  ", "").replace("：", "");
			String supplier = StringUtils.regexData(supplierHtmlText, "中标供应商名称[\\s\\S]+法人代表");
			if (StringUtils.isNullOrEmpty(supplier)) {
				supplier = StringUtils.regexData(supplierHtmlText, "中标、成交供应商名称[\\s\\S]+法人代表");
			}
			supplier = supplier.replace("中标供应商名称", "").replace("中标、成交供应商名称", "").replace("法人代表", "");
			resolve.setSupplier(supplier);
			
			//预算金额（元）
			String budgetHtmlText = htmlText.replace(",", "").replace("(", "（").replace(")", "）");
			String budget = StringUtils.regexData(budgetHtmlText, "采购项目预算金额（元）：([1-9]\\d*\\.?\\d*)|(0\\.\\d*[1-9])");
			budget = budget.replace("采购项目预算金额（元）：", "");
			if (budget.indexOf(",") != -1) {
				budget = budget.split(",")[0];
			}
			resolve.setBudget(budget);
			
			//成交额（元）
			String bidPrice = "";
			String bidPriceText = "";
			String bidPriceHtmlText = htmlText.replace(",", "");
			if (!StringUtils.isNullOrEmpty(htmlCode) && StringUtils.isNullOrEmpty(resolve.getBidPrice())) {
				String bidPriceHtmlCode = htmlCode.replace(",", "");
				Document doc = Jsoup.parse(bidPriceHtmlCode);
				Elements divElements = doc.select(".zdynr");
				try {
					bidPriceText = divElements.select("table").get(0).select("tbody > tr").get(1).select("td").get(5).text();
				} catch (Exception e) {
					try {
						bidPriceText = divElements.select("table").get(1).select("tbody > tr").get(1).select("td").get(5).text();
						if (StringUtils.isNullOrEmpty(bidPriceText)) {
							bidPriceText = divElements.select("table").get(1).select("tbody > tr").get(1).select("td > p").get(2).text();
						}
					} catch (Exception e1) {
						e1.printStackTrace();
						try {
							bidPriceText = divElements.select("table").get(1).select("tbody > tr").get(1).select("td > p").get(2).text();
						} catch (Exception e2) {
							e2.printStackTrace();
						}
					}
				}
				bidPrice = StringUtils.regexData(bidPriceText, "([1-9]\\d*\\.?\\d*)|(0\\.\\d*[1-9])");
				resolve.setBidPrice(bidPrice);
			} else if (StringUtils.isNullOrEmpty(resolve.getBidPrice())) {
				bidPriceText = StringUtils.regexData(bidPriceHtmlText, "中标金额：人民币([1-9]\\d*\\.?\\d*)|(0\\.\\d*[1-9])");
				bidPrice = bidPriceText.replace("中标金额：人民币", "");
				if (!StringUtils.isNullOrEmpty(bidPrice)) {
					resolve.setBidPrice(bidPrice);
				}
			}
			
			//发标时间
			String publishTime = StringUtils.regexData(htmlText, "招标公告日期：\\d{4}年\\d{1,2}月\\d{1,2}日");
			if (!StringUtils.isNullOrEmpty(publishTime)) {
				publishTime = publishTime.replace("招标公告期：", "");
			}
			resolve.setPublishTime(publishTime);
			
			//中标时间
			String dealTimeText = htmlText.replace("：", ":");
			String dealTime = StringUtils.regexData(dealTimeText, "评审日期：\\d{4}年\\d{1,2}月\\d{1,2}日");
			if (StringUtils.isNullOrEmpty(dealTime)) {
				dealTime = StringUtils.regexData(htmlCode, "评审日期：\\d{4}-\\d{1,2}-\\d{1,2}");
				if (StringUtils.isNullOrEmpty(dealTime)) {
					dealTime = StringUtils.regexData(dealTimeText, "评审时间:\\d{4}-\\d{1,2}-\\d{1,2} \\d{2}:\\d{2}:\\d{2}");
					dealTime = dealTime.replace("评审时间:", "");
					dealTime = DateUtils.dateToStringFormat(dealTime, DateUtils.dateformat5, DateUtils.dateformat14);
				} else {
					dealTime = dealTime.replace("评审日期：", "");
					dealTime = DateUtils.dateToStringFormat(dealTime, DateUtils.dateformat8, DateUtils.dateformat14);
				}
			}
			resolve.setDealTime(dealTime.replace("评审日期：", ""));
			
			//是否专项
			resolve.setSpecial(1);
		}
		return resolve;
	}
	
	/**
	 * html结果放进document对象
	 * @param result
	 * @return
	 */
	private Document getDocument(String result) {
		Document doc = Jsoup.parse(result);
		return doc;
	}
	
	/**
	 * 根据url截取网站id
	 * @param url
	 * @return
	 */
	private String getIdByUrl(String url) {
		String id = url.substring(url.lastIndexOf("/") + 1) ;
		id = id.replace("news-", "").replace(".html", "").replace(".htm", "");
		id  = id.substring(0, id.indexOf("-"));
		return id;
	}
	
	/**
	 * 根据网站名称查询最后一次爬取的公告发布时间
	 * @param siteName
	 * @return
	 */
	private String getNewPubdateBySiteName(String siteName, String keyword) {
		RecordBiddingNoticeDTO recordNotice;
		try {
			recordNotice = this.bidService.getNewPubdateBySiteName(siteName, keyword);
			String newPubdate = recordNotice.getPubdate();
			return newPubdate;
		} catch (Exception e) {
			e.printStackTrace();
			return "";
		}
	}
	
	public static void main(String[] args) throws ParseException {
//		BiddingInformationNetworkSpider spider = new BiddingInformationNetworkSpider();
//		Constants.IS_PROXY = true;
//		String url = "http://10.18.160.100:9090/api/notice/htmlCode/6913849?type=update";
//		url = "https://www.qichacha.com/cms_map?keyNo=2c06c9e6a55141a3d3b6338344d0ee69";
//		String result = spider.getHtmlContent(url);
//		System.out.println("result===>" + result);
		
//		Date manualDay = DateUtils.stringToDate("2018-08-27", DateUtils.dateformat8);
//		String time = DateUtils.getTime(-3, manualDay, DateUtils.dateformat8);
//		Date currentDay = DateUtils.stringToDate(DateUtils.getTime(-3, DateUtils.dateformat8), DateUtils.dateformat8);
//		System.out.println(time);
//		System.out.println(currentDay);
//		System.out.println(DateUtils.dateToString(currentDay, DateUtils.dateformat8));
		
		String url = "http://www.bidcenter.com.cn/news-50587250-4.html";
		String id = url.substring(url.lastIndexOf("/") + 1) ;
		id = id.replace("news-", "").replace(".html", "").replace(".htm", "");
		id  = id.substring(0, id.indexOf("-"));
		System.out.println("id=" + id);
	}
}